package com.css.influence.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.css.exception.GlobalErrorCodeConstants;
import com.css.influence.entity.EarthQuake;
import com.css.influence.entity.Model;
import com.css.influence.entity.SettingSeismicInfluenceflied;
import com.css.exception.CommonResult;
import com.css.influence.service.InfluenceService;
import com.css.influence.vo.EarthquakeModel;
import com.css.influence.vo.InfluenceTitleVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2024年09月11日  9:33
 * 影响场controller
 */
@RestController
@RequestMapping("/influence")
@Slf4j
public class InfluenceController {
    @Resource
    InfluenceService influenceService;

    /**
     * 根据地震事件查询关联的影响场
     */
    @GetMapping("/currentEarthInfluence/{earthId}/{current}/{type}")
    public CommonResult currentEarthInfluence(@PathVariable("earthId") String earthId,@PathVariable("current") String current,@PathVariable("type") Integer type,HttpServletRequest request){
        String code = request.getHeader("divisionCode");
        String divisionCode = code.split("_")[0];
        try{
            DynamicDataSourceContextHolder.push("province_"+divisionCode);
            List<InfluenceTitleVO> selectResult = influenceService.currentEarthInfluence(earthId,current,divisionCode);
            return CommonResult.success(selectResult);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DynamicDataSourceContextHolder.clear();
        }
        return null;
    }

    /**
     * 根据地震事件、影响场查询关联的影响场烈度信息
     * earthId:地震事件id
     * version:影响场版本
     * type :1 导出shp
     *       2 geojson
     */
    @PostMapping("/exportCurrentEarthInfluence")
    public void currentEarthInfluenceForExport(@RequestBody  Map<String,Object> map,
                                               HttpServletRequest request,
                                               HttpServletResponse response){
        try{
            String earthId = map.get("earthId").toString();
            String flag = map.get("flag").toString();
            String version = map.get("version").toString();
            Integer type = Integer.parseInt(map.get("type").toString());
            influenceService.currentEarthInfluenceForExport(earthId,version,type,flag,request,response);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 创建/重新生成地震影响场
     */
    @PostMapping("/renewSaveInfluence")
    public CommonResult renewSaveInfluence(@RequestBody Map<String,Object> map,HttpServletRequest request){
        //2 重新生成  3、修正
        try{
            String earthQuakeId = map.get("earthQuakeId").toString();
            int type = Integer.parseInt(map.get("type").toString());
            influenceService.createEarthQuakeInfluence(earthQuakeId,type,request);
        }catch (Exception e){
            return CommonResult.error("生成影响场失败");
        }
        return CommonResult.success(200,"生成影响场成功");
    }

    /**
     * 修正地震影响场
     */
    @PostMapping("/correctInfluence")
    public CommonResult correctInfluence(@RequestBody Map<String,Object> map,HttpServletRequest request){
        try{
            String version = influenceService.correctInfluence(map,request);
            return CommonResult.success(200,"修正成功");
        }catch (Exception ex){
            ex.printStackTrace();
            return CommonResult.error("修改失败");
        }
    }
    /**
     * 修正详情
     */
    @GetMapping("/correctDetail")
    public CommonResult correctDetail(@RequestParam("earthQuakeId") String earthQuakeId,
                                      @RequestParam("influenceVersion") String influenceVersion,
                                      HttpServletRequest request){
        try{
            return CommonResult.success(influenceService.correctDetail(earthQuakeId,influenceVersion,request));
        }catch (Exception e){
            log.error("get getAllRegion error! message:{}", e.getMessage(), e);
            CommonResult<Integer> error = CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode());
            error.setMsg("系统内部发生异常");
            return error;
        }

    }


    /**
     * 导入影响场 不需要单独再生成影响场文件
     */
    @PostMapping("/importInfluence")
    public CommonResult importInfluence(@RequestBody Map<String,Object> Map,HttpServletRequest request){
        try{
            influenceService.importInfluence(Map,request);
        }catch (Exception e){
            return CommonResult.error(e.getMessage());
        }
        return CommonResult.success(200,"导入成功");
    }


}
